package com.coderman.business.service.imp;

import com.coderman.business.converter.ExamTypeConverter;
import com.coderman.business.mapper.ExamTypeMapper;
import com.coderman.business.service.IExamTypeService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.error.SystemCodeEnum;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.student.ExamType;
import com.coderman.common.vo.student.ExamTypeVO;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 考试类型业务层实现
 * @author  文晓平
 * @Date 2023-1-9
 * @version 1.0
 */
@Service
public class ExamTypeServiceImpl extends AbstractServiceImpl<ExamTypeMapper, ExamType, Integer> implements IExamTypeService {
   @Autowired
   private ExamTypeMapper examTypeMapper;
    @Override
    public PageVO<ExamType> findList(Integer pageNum, Integer pageSize, ExamTypeVO examTypeVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(ExamType.class);
        String examTypeName = examTypeVO.getExamTypeName();
        Example.Criteria criteria = o.createCriteria();
        if(!StringUtils.isEmpty(examTypeName)){
            criteria.andLike("examTypeName","%"+examTypeName+"%");
        }
        List<ExamType> examTypeList= examTypeMapper.selectByExample(o);
        PageInfo<ExamType> info=new PageInfo<>(examTypeList);
        return new PageVO<>(info.getTotal(),examTypeList);
    }

    /**
     * 添加考试类型
     *
     * @param examTypeVO
     */
    @Override
    public void add(ExamTypeVO examTypeVO) {
        ExamType examType = new ExamType();
        BeanUtils.copyProperties(examTypeVO,examType);
        examTypeMapper.insert(examType);
    }

    /**
     * 编辑考试类型
     *
     * @param id
     * @return
     */
    @Override
    public ExamTypeVO edit(Integer id) throws SystemException {
        ExamType examType = examTypeMapper.selectByPrimaryKey(id);
        if(examType==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的考试类型不存在");
        }
        ExamTypeVO examTypeVO = ExamTypeConverter.converterToExamTypeVO(examType);
        return examTypeVO;
    }

    /**
     * 更新考试类型
     *
     * @param id
     * @param examTypeVO
     */
    @Override
    public void update(Integer id, ExamTypeVO examTypeVO) throws SystemException {
        ExamType dbExamType = examTypeMapper.selectByPrimaryKey(id);
        if(dbExamType==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的考试类型不存在");
        }
        ExamType examType = new ExamType();
        BeanUtils.copyProperties(examTypeVO,examType);
        examType.setExamTypeId(id);
        examTypeMapper.updateByPrimaryKeySelective(examType);
    }

    /**
     * 删除考试类型
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws SystemException {
        ExamType examType = examTypeMapper.selectByPrimaryKey(id);
        if(examType==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的考试类型不存在");
        }
        examTypeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有考试类型
     *
     * @return
     */
    @Override
    public List<ExamTypeVO> findAllVO() {
        List<ExamType> examTypes = examTypeMapper.selectAll();
        //转vo
        List<ExamTypeVO> examTypeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(examTypes)) {
            for (ExamType examType : examTypes) {
                ExamTypeVO e = new ExamTypeVO();
                BeanUtils.copyProperties(examType, e);
                Example o = new Example(ExamType.class);
                o.createCriteria().andEqualTo("examTypeId",examType.getExamTypeId());
                examTypeVOS.add(e);
            }
        }
        return examTypeVOS;
    }

    /**
     * 全部考试类型
     *
     * @return
     */
    @Override
    public List<ExamType> findAll() {
        return examTypeMapper.selectAll();
    }
}
